home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / IBTip / Source / StringResponder.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.5 KB  |  89 lines

  1. #import "StringResponder.h"
  2. #import <stdio.h>
  3. #import <stdlib.h>
  4. #import <string.h>
  5.  
  6. @implementation StringResponder
  7.   
  8. + new
  9. {
  10.   self = [super new];
  11.   list = (listElt *)malloc(sizeof(listElt));  // to simplify searching, list head never has valid data
  12.   list->next = NULL;
  13.   maxlen = 0;
  14.   return self;
  15. }
  16.  
  17. - free
  18. {
  19.   listElt *p, *q;
  20.  
  21.   p=list->next;
  22.   while (p) {
  23.     q=p->next;
  24.     free(p->s);
  25.     free(p);
  26.     p=q;
  27.   }
  28.   free(list);
  29.   return [super free];
  30. }
  31.  
  32. - addString:(char *)string returnValue:(int)tag
  33. {
  34.   listElt *p;
  35.   int len;
  36.  
  37.   len = strlen(string);
  38.   if (len>maxlen) maxlen=len;
  39.   for (p=list; p->next; p=p->next)
  40.     ;
  41.   p->next = (listElt *)malloc(sizeof(listElt));
  42.   p = p->next;
  43.   p->next = NULL;
  44.   p->s = (char *)malloc(len+1);
  45.   strcpy(p->s, string);
  46.   p->strlen = len;
  47.   if (tag==0) {
  48.     fprintf(stderr,"StringResponder:registered tag must be non-zero, will make it -1\n");
  49.     tag=(-1);
  50.   }
  51.   p->tag = tag;
  52.   p->matchedChars = 0;
  53.   return self;
  54. }
  55.  
  56. - (int) inputString:(char *)input
  57. {
  58.   int i,len;
  59.   BOOL seen;
  60.  
  61.   len=strlen(input);
  62.   seen = NO;
  63.   for (i=0; i<len && !seen; i++)
  64.     seen = [self inputChar:input[i]];
  65.   return seen;
  66. }
  67.  
  68. - (int) inputChar:(char)ichar
  69. {
  70.   listElt *p;
  71.   int m;
  72.  
  73.   for (p=list->next; p; p=p->next) {
  74.     m = p->matchedChars;
  75.     if ((p->s)[m] == ichar) {
  76.       (p->matchedChars)++;
  77.       if (p->strlen == m+1) {  // trigger is pulled
  78.     p->matchedChars = 0;
  79.     return p->tag;
  80.       }
  81.     }
  82.     else
  83.       p->matchedChars = 0;
  84.   }
  85.   return 0;
  86. }
  87.  
  88. @end
  89.